<?php
/**
 * Goods.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-15 下午1:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class Goods extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'Goods',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'AppleId' => self::VAR_TYPE_STRING,
            'Title' => self::VAR_TYPE_STRING,
            'Description' => self::VAR_TYPE_STRING,
            'Price' => self::VAR_TYPE_FLOAT,
            'JCoin' => self::VAR_TYPE_INT,
            'Donate' => self::VAR_TYPE_INT,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'Goods:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const MC_LIST_ALL_GOODS = 'LIST_ALL_GOODS';
    const UPDATE_DEL_CACHE_LIST = true; // 更新是否清除列表缓存
    protected static $_cache_list_keys = [
        self::MC_LIST_ALL_GOODS => self::CACHE_LIST_TYPE_MC,
    ];

    /**
     * 获取商品列表
     *
     * @return array
     */
    public static function getAllGoods()
    {
        $extra = [
            'type' => parent::MOD_TYPE_MEMCACHE,
            'mckey' => self::MC_LIST_ALL_GOODS,
            'mtime' => 86400,
            'fields' => '*',
            'listkey' => 'Id',
        ];

        return parent::getDataByFields([], $extra);
    }

    /**
     * 商品苹果商店id
     *
     * @param $apple_id
     * @return array
     */
    public static function getInfoByAppleId($apple_id)
    {
        $where = [
            'AppleId' => $apple_id
        ];
        $data = self::getList($where, 0, 1);

        return !empty($data['data'][0]) ? $data['data'][0] : [];
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}